package com.lf.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lf.DAO.courseDAO;
import com.lf.DAO.scDAO;
import com.lf.DAO.teacherDAO;
import com.lf.entity.shiti.course;
import com.lf.entity.shiti.sc;
import com.lf.entity.shiti.teacher;
import com.lf.impl.cou_impl;
import com.lf.impl.sc_impl;
import com.lf.impl.tea_impl;

/**
 * Servlet implementation class sc_Servlet
 */
@WebServlet("/sc_Servlet")
public class sc_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public sc_Servlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String action = request.getParameter("action");
		if(action==null){
			findAll(request,response);
		}
		switch (action) {
		case "findAll":
			findAll(request,response);
			break;
		case "find_stu_course":
			find_stu_course(request,response);
			break;
		case "find_stu_sc":
			find_stu_sc(request,response);
			break;
		case"find_tea_sc":
			find_tea_sc(request,response);
			break;
		case"find_tea_stu_sc":
			find_tea_stu_sc(request,response);
			break;
		case"add_sc":
			add_sc(request,response);
			break;
		case"update_sc":
			update_sc(request,response);
			break;
		case"delect_sc":
			delect_sc(request,response);
			break;
		default:
			break;
		}

	}
	private void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException{
		scDAO sd = new sc_impl();
		List<sc> list = sd.findAll();
		request.getSession().setAttribute("list", list);
		response.sendRedirect("test001.jsp");
	}

	private void find_stu_course(HttpServletRequest request, HttpServletResponse response) throws IOException{

		String s_id = new String(request.getParameter("s_id").getBytes(        "ISO-8859-1"), "utf-8");
		scDAO sd = new sc_impl();
		List<sc> list = sd.find_stu_course(s_id);
		request.getSession().setAttribute("list", list);
		response.sendRedirect("test001.jsp");
	}
	
	/**
	 * 根据Id查询该学生的成绩
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void find_stu_sc(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String s_id = request.getParameter("s_id");
		String s_name = request.getParameter("s_name");
		scDAO sd = new sc_impl();
		List<sc> list = sd.find_stu_sc(s_id);
		request.getSession().setAttribute("list", list);
		request.getSession().setAttribute("s_id", s_id);
		request.getSession().setAttribute("s_name", s_name);
		response.sendRedirect("Score.aspx.jsp");
	}
	private void find_tea_sc(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String t_id = new String(request.getParameter("t_id").getBytes(        "ISO-8859-1"), "utf-8");
		scDAO sd = new sc_impl();
		List<sc> list = sd.find_tea_sc(t_id);
		request.getSession().setAttribute("list", list);
		response.sendRedirect("test001.jsp");
	}
	private void find_tea_stu_sc(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String t_id = new String(request.getParameter("t_id").getBytes(        "ISO-8859-1"), "utf-8");
		scDAO sd = new sc_impl();
		List<sc> list = sd.find_tea_stu_sc(t_id);
		request.getSession().setAttribute("list", list);
		response.sendRedirect("test001.jsp");
	}
	private void add_sc(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String old_s_id = new String(request.getParameter("s_id").getBytes(        "ISO-8859-1"), "utf-8");
		int s_id = Integer.valueOf(old_s_id).intValue();
		String c_id = new String(request.getParameter("t_id").getBytes(        "ISO-8859-1"), "utf-8");
		String grade = new String(request.getParameter("t_id").getBytes(        "ISO-8859-1"), "utf-8");
		scDAO sd = new sc_impl();
		int count = sd.add_sc(s_id, c_id, grade);
		if (count>0) {
			System.out.print("成功");
		} else {
			System.out.print("失败");
		}

	}
	private void update_sc(HttpServletRequest request, HttpServletResponse response) throws IOException{

		String old_s_id = new String(request.getParameter("s_id").getBytes(        "ISO-8859-1"), "utf-8");
		int s_id = Integer.valueOf(old_s_id).intValue();
		String c_id = new String(request.getParameter("t_id").getBytes(        "ISO-8859-1"), "utf-8");
		String grade = new String(request.getParameter("t_id").getBytes(        "ISO-8859-1"), "utf-8");
		String old_sc_id = new String(request.getParameter("s_id").getBytes(        "ISO-8859-1"), "utf-8");
		int sc_id = Integer.valueOf(old_sc_id).intValue();
		scDAO sd = new sc_impl();
		int count = sd.update_sc(s_id, c_id, grade, sc_id);
		if (count>0) {
			System.out.print("成功");
		} else {
			System.out.print("失败");
		}
	}
	private void delect_sc(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String old_sc_id = new String(request.getParameter("s_id").getBytes(        "ISO-8859-1"), "utf-8");
		int sc_id = Integer.valueOf(old_sc_id).intValue();
		scDAO sd = new sc_impl();
		int count = sd.delect_sc(sc_id);
		if (count>0) {
			System.out.print("成功");
		} else {
			System.out.print("失败");
		}
	}
}

